package com.xacero.sw;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

public class SimpleWorldBoot {
   String master_dir;
   File config;
   SimpleWorldAPI swapi;

   public SimpleWorldBoot() {
      this.master_dir = (new File(".")).getAbsolutePath().substring(0, (new File(".")).getAbsolutePath().length() - 1) + File.separator + "plugins" + File.separator + "SimpleWorldPlugin";
      this.config = new File(this.master_dir + File.separator + "load.cfg");
      this.swapi = new SimpleWorldAPI();
   }

   public void loadWorlds() {
      Logger log = Logger.getLogger("minecraft");
      if (!(new File(this.master_dir)).exists()) {
         (new File(this.master_dir)).mkdir();
      }
      log.info("[Simple Worlds Reloaded] 插件原作者:Xacero 汉化&重置:huzpsb");
      log.info("[Simple Worlds Reloaded] 我们开始接管服务器的世界管理系统了");
      if (!this.config.exists()) {
         log.warning("没有找到配置文件.正在生成默认配置文件...");

         try {
            this.config.createNewFile();
            BufferedWriter w = new BufferedWriter(new FileWriter(this.config));
            w.write("1:world|normal");
            w.newLine();
            w.write("2:world_nether|nether");
            w.flush();
            w.close();
         } catch (IOException var8) {
            log.warning("生成配置文件失败 - " + var8);
         }
      }

      try {
         BufferedReader read = new BufferedReader(new FileReader(this.config));
         ArrayList world_list = new ArrayList();

         String inln;
         while((inln = read.readLine()) != null) {
            world_list.add(inln);
         }

         read.close();
         log.info("配置已经加载");

         for(int i = 0; i < world_list.size(); ++i) {
            String name = ((String)world_list.get(i)).split(":")[1];
            String environment = name.split("\\|")[1];
            name = name.split("\\|")[0];
            if (!this.swapi.doesWorldExist(name)) {
               log.info("自动加载世界:" + name + "不存在!");
            } else if (this.swapi.isWorldLoaded(name)) {
               log.info("自动加载世界:" + name + "被加载过了!");
            } else if (this.swapi.loadWorld(name, this.swapi.getEnvironmentFromString(environment))) {
               log.info("自动加载世界:" + name + "加载成功!");
            } else {
               log.info("自动加载世界:" + (String)world_list.get(i) + "时出现未知错误.");
            }
         }
         log.info("[Simple Worlds Reloaded] 加载完毕!");
      } catch (Exception var9) {
         log.warning("致命错误 - " + var9);
      }

   }
}
